package com.qms.repast.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qms.repast.R;
import com.qms.repast.entity.MessageEvent;
import com.qms.repast.entity.resbean.BannerBean.BodyBean.InfoBean;
import com.qms.repast.net.GlideImageLoader;
import com.qms.repast.ui.activity.SortActivity;
import com.qms.repast.ui.activity.WebActivity;
import com.qms.repast.ui.base.BaseFragment;
import com.qms.repast.ui.presenter.ReservePresenter;
import com.qms.repast.ui.view.IReserveView;
import com.qms.repast.weidgets.adsorptionhelper.HeaderViewPager;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author 泅渡者
 * @date 2018/9/3 0003
 * @describe: 订桌界面
 */
public class ReserveFragment extends BaseFragment<ReservePresenter> implements IReserveView {


	@BindView(R.id.banner)
	Banner banner;
	@BindView(R.id.iv_sort)
	ImageView ivSort;
	@BindView(R.id.rv_sort)
	TextView rvSort;
	@BindView(R.id.ll_sort)
	LinearLayout llSort;
	@BindView(R.id.course_checkbox)
	CheckBox courseCheckbox;
	@BindView(R.id.ll_float)
	LinearLayout llFloat;
	@BindView(R.id.fm_content)
	FrameLayout fmContent;
	@BindView(R.id.scrollableLayout)
	HeaderViewPager scrollableLayout;

	/**
	 * 餐厅列表内容布局
	 */
	private RecyclerViewFragment recyclerViewFragment;
	private Intent mIntent;

	@Override
	protected int initLayout () {
		return R.layout.fragment_reserve;
	}

	@Override
	protected void initPresenter () {
		mPresenter = new ReservePresenter(mActivity, this);
	}

	@Override
	protected void pullData () {
	}

	@Override
	protected void initView (View view, Bundle savedInstanceState) {

		recyclerViewFragment = new RecyclerViewFragment();
		FragmentManager fm = getFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
		transaction.replace(R.id.fm_content, recyclerViewFragment);
		transaction.commit();

		scrollableLayout.setCurrentScrollableContainer(recyclerViewFragment);
		scrollableLayout.setOnScrollListener(new HeaderViewPager.OnScrollListener() {
			@Override
			public void onScroll (int currentY, int maxY) {

			}
		});

		courseCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged (CompoundButton compoundButton, boolean isChanged) {
				if (isChanged) {
					EventBus.getDefault().post(new MessageEvent(2011, "gride"));
				} else {
					EventBus.getDefault().post(new MessageEvent(2012, "list"));
				}
			}
		});

		//加载轮播
		mPresenter.getBannerList();
	}


	@OnClick({R.id.ll_sort})
	public void onViewClicked (View view) {
		switch (view.getId()) {
			case R.id.ll_sort:
				startActivity(new Intent(getHoldingActivity(), SortActivity.class));
				break;
			default:
				break;
		}
	}

	@Override
	public void initBanner (final List<InfoBean> bannerList) {
		List<String> images = new ArrayList<>();
		List<String> titles = new ArrayList<>();
		for (InfoBean bannerInfo : bannerList) {
			images.add(bannerInfo.getPicUrl());
			titles.add(bannerInfo.getTitle());
		}
		//设置banner样式
		banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
		//设置图片加载器
		banner.setImageLoader(new GlideImageLoader());
		//设置图片集合
		banner.setImages(images);
		//设置banner动画效果
		banner.setBannerAnimation(Transformer.Default);
		//设置标题集合（当banner样式有显示title时）
		banner.setBannerTitles(titles);
		//设置自动轮播，默认为true
		banner.isAutoPlay(true);
		//设置轮播时间
		banner.setDelayTime(3200);
		//设置指示器位置（当banner模式中有指示器时）
		banner.setIndicatorGravity(BannerConfig.CENTER);
		//banner设置方法全部调用完毕时最后调用
		banner.start();
		banner.setOnBannerListener(new OnBannerListener() {
			@Override
			public void OnBannerClick (int position) {
				InfoBean infoBean = bannerList.get(position);
				String webView = "1";
				//跳转至WebView界面
				if (webView.equals(infoBean.getType())) {
					mIntent = new Intent();
					mIntent.setClass(getHoldingActivity(), WebActivity.class);
					mIntent.putExtra("loadUrl", infoBean.getTarget());
					mIntent.putExtra("title", infoBean.getTitle());
					startActivity(mIntent);
				}
				//跳转至店铺界面
				else {
					mIntent = new Intent();
					mIntent.setClass(getHoldingActivity(), WebActivity.class);
					mIntent.putExtra("loadUrl", infoBean.getTarget());
					mIntent.putExtra("title", infoBean.getTitle());
					startActivity(mIntent);
				}
			}
		});

	}
}
